package com.pets.service.Impl;

import com.pets.dao.AnimalMapper;
import com.pets.dao.KnowledgeMapper;
import com.pets.entity.Animal;
import com.pets.entity.Knowledge;
import com.pets.enums.YesNoEnum;
import com.pets.qo.AnimalAddQO;
import com.pets.qo.KnowledgeAddQO;
import com.pets.service.AnimalService;
import com.pets.service.KnowledgeService;
import com.pets.util.PageData;
import com.pets.util.Result;
import com.pets.util.UploadUtil;
import com.pets.vo.AnimalVO;
import com.pets.vo.KnowledgesByPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class AnimalServiceImpl implements AnimalService {
    @Autowired
    private AnimalMapper animalDao;

    //分页查询
    @Override
    public PageData<AnimalVO> selectAnimalsByPage(int page, int pageSize) {
        PageData<AnimalVO> pd = new PageData<>();
        pd.setPage(page);
        pd.setPageSize(pageSize);
        //page是页数   pageSize 每页得条数
        List<AnimalVO> animalsByPage = animalDao.selectAnimalsByPage((page-1)*pageSize, pageSize);
        int total = animalDao.selectAnimalsCount();
        pd.setTotal(total);
        pd.setTotalPage(pd.getTotalPage());
        if (animalsByPage.size() > 0) {
            pd.setData(animalsByPage);
        }
        return pd;
    }

    //流浪动物的添加操作
    public Result AnimalAdd( AnimalAddQO animalAddQO){
        Result result=new Result();
        //做文件上传动作
        String url ;
        try {
             url = UploadUtil.uploadFile(animalAddQO.getFile());
        } catch (IOException e) {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("文件上传失败");
            e.printStackTrace();
            return result;
        }
        Animal animal=new Animal();
        animal.setAname(animalAddQO.getaName());
        animal.setAage(animalAddQO.getaAge());
        animal.setAsex(animalAddQO.getaSex());
        animal.setAstatus(0);
        animal.setUid(animalAddQO.getuId());
        animal.setAhealthcondition(animalAddQO.getaHealthCondition());
        animal.setAspecies(animalAddQO.getaSpecies());
        animal.setAintroduction(animalAddQO.getaIntroduction());
        animal.setApicture(url);
        int i=animalDao.insert(animal);
        if(i >0){
            result.setCode(YesNoEnum.YES.getCode());
            result.setMsg("流浪动物添加成功");
            return result;
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("流浪动物添加失败");
            return result;
        }

    }

     /**
     * 单条流浪动物信息的显示
     * @param aId
     * @return
     */
    public AnimalVO show(int aId){
        AnimalVO a=animalDao.selectAnimalByAid(aId);
        if(a!=null) {
            return a;
        }
        return null;
    }

    @Override
    public List<Animal> getUserAnimal(int uId) {
        return animalDao.getUserAnimal(uId);
    }
}
